<?php

namespace app\model\store;

use app\base\BaseModel;
use app\model\order\PayLogModel;
use think\facade\Cache;
use think\facade\Db;

/**
 * 开店申请 保证金缴纳
 * 模型-StoreDepositModel
 * Class StoreDepositModel
 * @package app\model
 */
class StoreDepositModel extends BaseModel
{

    protected $name = 'store_deposit';

    /**
     * 缴纳保证金
     * @param $store_id
     * @param $user_id
     * @param $price
     * @return array
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addDepositPayInfo($store_id, $user_id, $price = 1000)
    {
        $order_id = $this->addDeposit($store_id, $user_id, $price);
        $pay_info = [];
        if ($order_id !== false) {
            $pay_info = $this->getDepositPayInfo($order_id, $user_id);
        }
        return $pay_info;
    }

    /**
     * 添加店铺保证金缴纳记录
     * @param $store_id
     * @param $user_id
     * @param $price
     * @param $pay_mode
     * @return bool|mixed
     * @throws \Exception
     */
    public function addDeposit($store_id, $user_id, $price, $pay_mode = 'weixin')
    {
        $map = [
            'store_id' => $store_id,
            'status' => 0,
            'user_id' => $user_id
        ];
        $this->where($map)->delete();
        $order_sn = date("YmdHis") . $store_id . $user_id . mt_rand(1000, 9999);
        $in_data = [
            'order_sn' => $order_sn,
            'user_id' => $user_id,
            'store_id' => $store_id,
            'pay_mode' => $pay_mode,
            'price' => $price,
            'add_time' => time(),
            'add_date' => date("Y-m-d H:i:s")
        ];
        $ret = $this->insert($in_data);
        if ($ret != false) {
            $order_id = $this->getLastInsID();
            return $order_id;
        }
        return false;
    }

    /**
     * 获取保证金订单支付信息
     * @param $order_id
     * @param $user_id
     * @param string $pay_mode
     * @return array
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDepositPayInfo($order_id, $user_id, $pay_mode = 'weixin')
    {
        $pay_sn = Cache::store('redis')->get($this->redis_key . 'deposit' . $order_id, 0);
        if (empty($pay_sn)) {
            $pay_sn = "deposit" . date("YmdHis") . $order_id . rand(1000, 9999);
            Cache::store('redis')->set($this->redis_key . 'deposit' . $order_id, $pay_sn, 600);
        }
        $map = [
            'id' => $order_id,
            'user_id' => $user_id,
            'status' => 0
        ];
        $order_data = $this->where($map)->find();
        if (empty($order_data)) {
            throw new \think\Exception("支付信息不存在！", 0);
        }
        $order_id = $order_data['id'];
        $order_sn = $order_data['order_sn'];
        Db::startTrans();
        $order_total = $order_data['price'];
        $up_pay = [
            'pay_sn' => $pay_sn,
        ];
        $ret = $this->where($map)->update($up_pay);
        $out_data = [
            'order_id' => $order_id,
            'order_sn' => $order_sn,
            'pay_sn' => $pay_sn,
            'total' => $order_total
        ];
        if ($ret !== false) {
            PayLogModel::addLog($out_data, "deposit", $pay_mode);
            Db::commit();
        } else {
            Db::rollback();
            return [];
        }
        return $out_data;
    }

    /**
     * 完成保证金支付订单
     * @param $pay_sn
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function endDepositPay($pay_sn)
    {
        $map = [
            'pay_sn' => $pay_sn
        ];
        $deposit = $this->where($map)->field('user_id,store_id,price')->find();
        $up_pay = [
            'status' => 1
        ];
        $ret = $this->where($map)->update($up_pay);
        if ($ret !== false) {
            $store_model = new StoreModel();
            $up_data = [
                'store_deposit' => $deposit['price']
            ];
            $ret = $store_model->where('id', $deposit['store_id'])->update($up_data);
            $store_auth_user_model = new StoreAuthUserModel();
            $store_auth_user_model->activateManage($deposit['store_id']);
            if ($ret !== false) {
                return true;
            }
        }
        return false;
    }

    /**
     * 退还保证金
     * @param $pay_sn
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function returnDeposit($pay_sn)
    {
        $map = [
            'pay_sn' => $pay_sn
        ];
        $deposit = $this->where($map)->field('user_id,store_id,price')->find();
        $up_pay = [
            'status' => 1
        ];
        $ret = $this->where($map)->update($up_pay);
        if ($ret !== false) {
            $store_model = new StoreModel();
            $up_data = [
                'store_deposit' => 0
            ];
            $ret = $store_model->where('id', $deposit['store_id'])->update($up_data);
            if ($ret !== false) {
                return true;
            }
        }
        return false;
    }


}
